package com.xunwulian.cmeb.command;

import com.google.gson.Gson;
import com.xunwulian.base.constant.CardInfoType;
import com.xunwulian.base.entity.CardInfoEntity;
import com.xunwulian.base.exception.ErrorCodeException;
import org.json.JSONArray;

public class CardInfoCommand extends Command {

    String cardinfo = "";
    CardInfoType type = null;

    public CardInfoCommand(Receiver receiver, String cardInfo, CardInfoType type) {
        super(APICode.CARDINFO, receiver);
        this.cardinfo = cardInfo;
        this.type = type;
    }

    @Override
    public String getParam() {
        return "card_info=" + cardinfo + "&type=" + type.getType();
    }

    @Override
    public CardInfoEntity execute() throws ErrorCodeException {
        JSONArray result = receiver.actionPost(api, getParam());

        CardInfoEntity entity = null;
        if (null != result && result.length() != 0) {
            Gson gson = new Gson();
            entity = gson.fromJson(result.getJSONObject(0).toString(), CardInfoEntity.class);
            // ICCID转变为大写
            entity.setIccid(entity.getIccid().toUpperCase());
        }
        return entity;
    }
}
